package games;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

public class Player extends JButton{
    String name;
    double money;
    Vector<Property> myProperties;
    boolean bankruptcy;
    DLNode<Square> position;
    boolean injail;

    public Player(DLNode<Square> p, String n){
        position = p;
        position.getData().add(this);
    	name=n;
        this.setText(name);
        position.getData().setSize(getMaximumSize());
        
    	bankruptcy=false;
    	money=15000;
        injail = false;
    	myProperties=new Vector<Property>();
        System.out.println("Player " + name +" created.");
    }

    String getPlayerName(){
        return name;
    }

    /*int getPosition(){
        return position;
    }
    void setPosition(int p){
        position = p;
    }*/
    
    public void move(int step){
        position.getData().remove(this);
    	if(step<0){
    		for(int i=0; i<step;i++){
        		position=position.getPrevNode();
        	}
    	}
    	else{
    		for(int i=0; i<step;i++){
    			position=position.getNextNode();
    		}
    	}
        position.getData().add(this);
    	position.getData().onSquare(this);
    }

    boolean isInJail(){
        if (injail == true){
            System.out.println("Do you wanna pay money(200) for feel free(y/n)?");

            int temp = JOptionPane.showConfirmDialog(
                    new JFrame(),
                    "Do you wanna buy "+name+" ?",
                    "No Owner",
                    JOptionPane.YES_NO_OPTION);


            if(temp == 0){
                if(this.loseMoney(200)){
                    return false;// feel free
                }
                else{
                    return injail;//true
                }
            }
            else {
                return injail;//true
            }
        }
        else{
            return injail;//true
        }
    }

    public void setInjail(boolean j) {
        this.injail = j;
    }

    public boolean loseMoney(double m){
        Scanner in = new Scanner(System.in);
        if(money<m){
            double totalMoney = this.money;
            for(int i=0;i<this.myProperties.size();i++){
                totalMoney+=this.myProperties.elementAt(i).ipotecPrice;
            }
            if(totalMoney<m){
                this.setBankruptcy(true);//iflas
                System.out.println("You loss this game, you will never win again");
                return false;
            }
            else{
                while (this.money>=m){
                    System.out.println("What you want to sell?");
                    for(int j=0;j<this.myProperties.size();j++){
                        System.out.println("Number : " + j + "name : " + this.myProperties.elementAt(j).name + "Price : " + this.myProperties.elementAt(j).ipotecPrice);
                    }
                    System.out.println("Please enter a numbers of properties which you want to sell: ");
                    String list;

                    //list = in.nextLine();
                    //JCheckBox checkbox = new JCheckBox("Do not show this message again.");

                    String[] allp = new String[myProperties.size()];
                    for (int i=0;i<myProperties.size();i++){
                        allp[i] = myProperties.elementAt(i).name;
                    }
                    JList mylist = new JList(allp);

                    JOptionPane.showMessageDialog(
                            null, mylist, "Multi-Select Example", JOptionPane.PLAIN_MESSAGE);
                    System.out.println(Arrays.toString(mylist.getSelectedIndices()));

                    for (int i=0;i<Arrays.asList(mylist.getSelectedIndices()).size();i++){
                        this.earnMoney(this.myProperties.elementAt(i).ipotecPrice);
                        this.myProperties.elementAt(i).owner=null;
                        this.myProperties.remove(i);
                    }


                    /*String message = "Are you sure you want to disconnect the selected products?";
                    Object[] params = {message, checkbox};
                    int n = JOptionPane.showConfirmDialog(view.propertiesDialog, params, "Disconnect Products", JOptionPane.YES_NO_OPTION);
                    boolean dontShow = checkbox.isSelected();


                    List<String> items = Arrays.asList(list.split("\\s*,\\s*"));

                    for(int k=0;k<items.size();k++){
                        int c = Integer.parseInt(items.get(k));
                        this.earnMoney(this.myProperties.elementAt(c).ipotecPrice);
                        this.myProperties.elementAt(c).owner=null;
                        this.myProperties.remove(c);
                    }*/

                }
                money -=m;
                return true;
            }
        }
        else{
            money -=m;
            return true;
        }
    }

    public void earnMoney(double m){
        money +=m;
    }
    
    public void addProperty(Property property){
    	myProperties.add(property);
    }
    
    public double getMoney(){
    	return money;
    }
    
    public void setBankruptcy(boolean b){
    	bankruptcy=b;
    }
    
    public boolean getBankruptcy(){
    	return bankruptcy;
    }

    public void isGroupComplited(Property p) {
        int size=0;
        for (int i = 0; i < myProperties.size(); i++) {
            if (p.groupId == myProperties.elementAt(i).groupId){
                size++;
            }
        }
        if (size ==3){
            for (int i = 0; i < myProperties.size(); i++) {
                if (p.groupId == myProperties.elementAt(i).groupId){
                    myProperties.elementAt(i).groupComplited = true;
                }
            }
        }
    }
}

